#include "Cube.h"

const short VERTEX_COUNT = 24;
const short INDEX_COUNT = 36;
const short PRIM_COUNT = INDEX_COUNT / 3;

/*-----------------------------------------------------------------------------------------------*/
Cube::Cube(std::string name, DWORD color) : Entity(name), m_color(color)
{
   m_pVertices = new NormalVertex[VERTEX_COUNT];

   m_pVertices[0].setVertex(-50.0f, -50.0f,  50.0f, 0.0f, 0.0f,  1.0f, m_color);
   m_pVertices[1].setVertex( 50.0f, -50.0f,  50.0f, 0.0f, 0.0f,  1.0f, m_color);
   m_pVertices[2].setVertex(-50.0f,  50.0f,  50.0f, 0.0f, 0.0f,  1.0f, m_color);
   m_pVertices[3].setVertex( 50.0f,  50.0f,  50.0f, 0.0f, 0.0f,  1.0f, m_color);
                                                                       
   m_pVertices[4].setVertex(-50.0f, -50.0f, -50.0f, 0.0f, 0.0f, -1.0f, m_color);
   m_pVertices[5].setVertex(-50.0f,  50.0f, -50.0f, 0.0f, 0.0f, -1.0f, m_color);
   m_pVertices[6].setVertex( 50.0f, -50.0f, -50.0f, 0.0f, 0.0f, -1.0f, m_color);
   m_pVertices[7].setVertex( 50.0f,  50.0f, -50.0f, 0.0f, 0.0f, -1.0f, m_color);
                                                                       
   m_pVertices[8].setVertex(-50.0f,  50.0f, -50.0f, 0.0f, 1.0f,  0.0f, m_color);
   m_pVertices[9].setVertex(-50.0f,  50.0f,  50.0f, 0.0f, 1.0f,  0.0f, m_color);
   m_pVertices[10].setVertex( 50.0f,  50.0f, -50.0f,  0.0f, 1.0f,  0.0f, m_color);
   m_pVertices[11].setVertex( 50.0f,  50.0f,  50.0f,  0.0f, 1.0f,  0.0f, m_color);
                                                                       
   m_pVertices[12].setVertex(-50.0f, -50.0f, -50.0f,  0.0f, -1.0f, 0.0f, m_color);
   m_pVertices[13].setVertex( 50.0f, -50.0f, -50.0f,  0.0f, -1.0f, 0.0f, m_color);
   m_pVertices[14].setVertex(-50.0f, -50.0f,  50.0f,  0.0f, -1.0f, 0.0f, m_color);
   m_pVertices[15].setVertex( 50.0f, -50.0f,  50.0f,  0.0f, -1.0f, 0.0f, m_color);
                                                                       
   m_pVertices[16].setVertex( 50.0f, -50.0f, -50.0f,  1.0f,  0.0f, 0.0f, m_color);
   m_pVertices[17].setVertex( 50.0f,  50.0f, -50.0f,  1.0f,  0.0f, 0.0f, m_color);
   m_pVertices[18].setVertex( 50.0f, -50.0f,  50.0f,  1.0f,  0.0f, 0.0f, m_color);
   m_pVertices[19].setVertex( 50.0f,  50.0f,  50.0f,  1.0f,  0.0f, 0.0f, m_color);
                                                                       
   m_pVertices[20].setVertex(-50.0f, -50.0f, -50.0f, -1.0f,  0.0f, 0.0f, m_color);
   m_pVertices[21].setVertex(-50.0f, -50.0f,  50.0f, -1.0f,  0.0f, 0.0f, m_color);
   m_pVertices[22].setVertex(-50.0f,  50.0f, -50.0f, -1.0f,  0.0f, 0.0f, m_color);
   m_pVertices[23].setVertex(-50.0f,  50.0f,  50.0f, -1.0f,  0.0f, 0.0f, m_color);

   USHORT indices[] =
    {
       0, 1, 2,     // side 1
       2, 1, 3,

       4, 5, 6,     // side 2
       6, 5, 7,

       8, 9, 10,    // side 3
       10, 9, 11,

       12, 13, 14,  // side 4
       14, 13, 15,

       16, 17, 18,  // side 5
       18, 17, 19,

       20, 21, 22,  // side 6
       22, 21, 23,
    };

   m_pIndices = new UINT[INDEX_COUNT];
   
   for (int i = 0; i < INDEX_COUNT; i++)
   {
      m_pIndices[i] = indices[i];
   }
}

void Cube::setMaterial()
{
   m_pMaterial.Diffuse = D3DXCOLOR(1,1,1,1);
   m_pMaterial.Ambient = D3DXCOLOR(1,1,1,1); 
   m_pMaterial.Emissive = D3DXCOLOR(0,0,0,0);
   m_pMaterial.Specular = D3DXCOLOR(0,0,0,0);
   m_pMaterial.Power = 0;
}

void Cube::draw(Renderer* renderer)
{
   Entity::draw(renderer);

   if ( isVisible() )
   {
      renderer->setMaterials(&m_pMaterial);
      renderer->DrawElements(VERTEX_COUNT, m_pVertices, INDEX_COUNT, m_pIndices, PRIM_COUNT, TRIANGLE_LIST);
   }
}